<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>事件处理方法-ref求和Demo</title>
    <script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script>
  </head>
  <body>
    <div id="app">
      <div ref="refHtml" @click="handlerHtml">ref的使用</div>
      <counter-item ref="one" @change="handleCounter"></counter-item>
      <counter-item ref="two" @change="handleCounter"></counter-item>
      <div>Counter: {{counter}}</div>
    </div>

    <script>
      Vue.component('counter-item', {
        template: '<div @click="handleAdd">Number: {{number}}</div>',
        data: function() {
          return {
            number: 0
          }
        },
        methods: {
          handleAdd: function() {
            this.number++;
            this.$emit('change');
          }
        },
      });
      var vm = new Vue({
        el: "#app",
        data: {
          counter: 0
        },
        methods: {
          handlerHtml: function() {
            alert(this.$refs.refHtml.innerText);
          },
          handleCounter: function() {
            this.counter = this.$refs.one.number + this.$refs.two.number;
          }
        },
      });
    </script>
  </body>
</html>
